var port = require('../../modules/port.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    author: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    wx.showNavigationBarLoading();
    var optId = option.id;
    var that = this;
    wx.request({
      url: port.dataPort.author,
      data: { authorId: optId },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT    
      // header: {}, // 设置请求的 header    
      success: function (data) {
        console.log(data.data.data.information)
        var description = '';
        if (data.data.data.information.length > 130) {
          description = that.cutstr(data.data.data.information, 210);
          that.setData({
            aboutStatus: true,
            retate: false
          })
        } else {
          description = data.data.data.information;
          that.setData({
            aboutStatus: false,
            retate: true
          })
        }
        that.setData({
          author: data.data.data,
          information: description
        })
        wx.hideNavigationBarLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  cutstr: function (str, len) {
    var str_length = 0;
    var str_len = 0;
    var str_cut = new String();
    var str_len = str.length;
    for (var i = 0; i < str_len; i++) {
      var a = str.charAt(i);
      str_length++;
      if (escape(a).length > 4) {
        str_length++;
      }
      str_cut = str_cut.concat(a);
      if (str_length >= len) {
        str_cut = str_cut.concat("...");
        return str_cut;
      }
    }
    if (str_length < len) {
      return str;
    }
  },
  aboutMore: function () {
    var aboutText = this.cutstr(this.data.author.information, 210)
    if (!this.data.retate) {
      this.setData({
        information: this.data.author.information,
        retate: true
      })
    } else {
      this.setData({
        information: aboutText,
        retate: false
      })
    }
  },
  articleTo: function (e) {
    wx.redirectTo({
      url: '../article/article?id=' + e.currentTarget.dataset.id
    })
  }
})